var user_id = sessionStorage.getItem('user_id')
var user_role = sessionStorage.getItem('user_role');
//////////////////////定义侧边栏名称 begin //////////////////////////
let user_manage = document.getElementById('user_manage')
let project_manage = document.getElementById('project_manage')
let process = document.getElementById('process')
let notice = document.getElementById('notice')
let money = document.getElementById('money')
let meeting = document.getElementById('meeting')
let document_manage = document.getElementById('document_manage')
let contract = document.getElementById('contract')
//获取申请项目的div
let apply = document.getElementById('applyBtn');
let join = document.getElementById('joinBtn');
//////////////////////定义侧边栏名称 end //////////////////////////

function showName() {
    document.querySelector('#yourName').innerHTML = sessionStorage.getItem('user_name')
}
showName()


///////////////////////////组员：加入项目组 begin ///////////////////////
var joinProject=function (id, projectName){
    var leaderID = id;
    var memberID = sessionStorage.getItem('user_id');
    var r = confirm("您确认加入项目："+ projectName +"?");
    if(r)
    {
        data = {}
        data.leaderID = leaderID;
        data.memberID = memberID;
        data.action = "join_project";
        $.post("./project/action", data, function (json) {
            json = JSON.parse(json);
            if(json.result == 1){
                alert("加入项目成功！");
                showPage();
                $('#project_join_form').modal('hide');
            }
            else alert("加入项目失败！");
        })
    }
}
///////////////////////////组员：加入项目组 end ///////////////////////

///////////////////////////组员：获取项目信息列表 begin ////////////////
var getProjectList=function (){
    data = {}
    data.action = "get_project_list"
    $.post("./project/action",data,function (json) {
        json = JSON.parse(json);
        console.log(json)
        if(json.result == 1)
        {
            var list = json.aaData;
            console.log(list)
            var html = "";
            if(list!=undefined && list.length > 0)
            {
                for(var i=0; i < list.length; i++){
                    var record = list[i];
                    html=html+"	<tr>";
                    html=html+"		<th>";
                    html=html+"			"+(i+1);
                    html=html+"		</th>";
                    html=html+"		<th>";
                    html=html+"			"+record.projectName;
                    html=html+"		</th>";
                    html=html+"		<th>";
                    html=html+"			"+record.leaderName;
                    html=html+"		</th>";
                    html=html+"		<th>";
                    html=html+"			"+"<button type=\"button\" class=\"btn default\" onclick='joinProject("+record.id+",\""+record.projectName+"\");'>加入</button>";
                    html=html+"		</th>";
                    html=html+"	</tr>";
                }
            }
            $("#project_list_table").html(html);
        }
    })

}
///////////////////////////组员：获取项目信息列表 end ////////////////
///////////////////////待办事项/////////////////////////////////////////////////待办事项 begin //////////////////////////
///////////////////////获取会议信息 begin //////////////////////////
getMeeting=function (){
    data = {}
    data.id = user_id;
    data.role = user_role;
    data.action = "query"
    $.post("./project/meet",data,function (json) {
        json = JSON.parse(json);
        console.log(json)
        if(json.meetings.length !== 0)
        {
            var list = json.meetings;
            console.log(list)
            var html = "";
            if(list!==undefined && list.length > 0)
            {
                for(var i=0; i < list.length; i++){
                    var record = list[i];
                    html=html+
                        "<li>" +
                        "<div class=\"col1\">" +
                        "<div class=\"cont\">" +
                        "<div class=\"cont-col1\">" +
                        "<div class=\"label label-sm label-danger\">" +
                        "<i class=\"fa fa-user\"></i>" +
                        "</div>" +
                        "</div>" +
                        "<div class=\"cont-col2\">" +
                        "<div class=\"desc\">" +
                        "新会议：" + record.title+
                        "</div>" +
                        "</div>" +
                        "</div>" +
                        "</div>" +
                        "<div class=\"col2\">" +
                        "<div>" +
                        +record.meetTime+
                        "</div>" +
                        "</div>" +
                        "</li>"
                }
            }
            $("#meeting_list").html(html);
        }
    })
}

///////////////////////获取会议信息 end //////////////////////////

///////////////////////获取通知信息 begin //////////////////////////
getNotice=function (){
    data = {}
    data.id = user_id;
    data.role = user_role;
    data.action = "query"
    $.post("./project/notice",data,function (json) {
        json = JSON.parse(json);
        console.log(json)
        if(json.list.length !== 0)
        {
            var list = json.list;
            console.log(list)
            var html = "";
            if(list!==undefined && list.length > 0)
            {
                for(var i=0; i < list.length; i++){
                    var record = list[i];
                    html=html+
                        "<li>" +
                        "<div class=\"col1\">" +
                        "<div class=\"cont\">" +
                        "<div class=\"cont-col1\">" +
                        "<div class=\"label label-sm label-default\">" +
                        "<i class=\"fa fa-bell-o\"></i>" +
                        "</div>" +
                        "</div>" +
                        "<div class=\"cont-col2\">" +
                        "<div class=\"desc\">新通知：" +
                        record.title+"   <span class=\"label label-sm label-default \">" +
                        +record.content+"</span>" +
                        "</div>" +
                        "</div>" +
                        "</div>" +
                        "</div>" +
                        "<div class=\"col2\">" +
                        "<div>" +
                        +record.time+
                        "</div>" +
                        "</div>" +
                        "</li>"
                }
            }
            $("#notice_list").html(html);
        }
    })
}

///////////////////////获取通知信息 end //////////////////////////
///////////////////////获取任务信息 begin //////////////////////////
getTaskInfo=function (){
    data = {}
    data.user_id = user_id;
    data.action = "get_task_info"
    $.post("./project/action1",data,function (json) {
        json = JSON.parse(json);
        console.log(json)
        if(json.result === 1)
        {
            var list = json.aaData;
            console.log(list)
            var html = "";
            if(list!==undefined && list.length > 0)
            {
                for(var i=0; i < list.length; i++){
                    var record = list[i];
                    if(record.progress != 100){
                        html=html+
                            "<li>" +
                            "<div class=\"col1\">" +
                            "<div class=\"cont\">" +
                            "<div class=\"cont-col1\">" +
                            "<div class=\"label label-sm label-info\">" +
                            "<i class=\"fa fa-check\"></i>" +
                            "</div>" +
                            "</div>" +
                            "<div class=\"cont-col2\">" +
                            "<div class=\"desc\">" +
                            record.content+"  <span class=\"label label-sm label-warning \">" +
                            "进度："+record.progress+"%<i class=\"fa fa-share\"></i>" +
                            "</span>" +
                            "</div>" +
                            "</div>" +
                            "</div>" +
                            "</div>" +
                            "<div class=\"col2\">" +
                            "<div>" +
                            +record.dueTime+
                            "</div>" +
                            "</div>" +
                            "</li>"
                    }

                }
            }
            $("#todo_list").html(html);
        }
    })
}
///////////////////////获取任务信息 end //////////////////////////
///////////////////////不同角色的个性化页面 begin //////////////////////////
function showPage() {
    getMeeting();
    getNotice();
    if (user_role === '组长') {
        join.style.display = 'none';
        data = {}
        data.action = "refresh";
        data.userID = sessionStorage.getItem('user_id');
        data.userRole = sessionStorage.getItem('user_role');
        data.userName = sessionStorage.getItem('user_login_name');
        $.post("./project/action",data,function (json) {
            json = JSON.parse(json);
            if(json.msg==='no'){
                $('#project_add_form').modal('show')
            }
            else{
                apply.style.display = 'none'
                join.style.display = 'none'
                user_manage.style.display = 'block'
                project_manage.style.display = 'block'
                process.style.display = 'block'
                notice.style.display = 'block'
                money.style.display = 'block'
                meeting.style.display = 'block'
                document_manage.style.display = 'block'
                contract.style.display = 'block'
            }
        })
    } else if (user_role === '组员') {
        apply.style.display = 'none';
        data = {}
        data.action = "refresh";
        data.userID = sessionStorage.getItem('user_id');
        data.userRole = sessionStorage.getItem('user_role');
        data.userName = sessionStorage.getItem('user_login_name');
        $.post("./project/action",data,function (json) {
            json = JSON.parse(json);
            if(json.msg==='no'){
                getProjectList();//获取项目信息
                $('#project_join_form').modal('show')
            }
            else{
                ///任务
                getTaskInfo();
                apply.style.display = 'none'
                join.style.display = 'none';
                user_manage.style.display = 'none'
                project_manage.style.display = 'none'
                process.style.display = 'block'
                notice.style.display = 'block'
                money.style.display = 'none'
                meeting.style.display = 'block'
                document_manage.style.display = 'block'
                contract.style.display = 'none'
            }
        })
    }
    else if (user_role === '管理员') {
        apply.style.display = 'none'
        join.style.display = 'none';

        user_manage.style.display = 'block'
        project_manage.style.display = 'block'
        process.style.display = 'block'
        notice.style.display = 'block'
        money.style.display = 'block'
        meeting.style.display = 'block'
        document_manage.style.display = 'block'
        contract.style.display = 'block'
    }
}
showPage()
///////////////////////不同角色的个性化页面 begin //////////////////////////





document.getElementById('project_add_button').addEventListener('click', function () {
    $('#project_add_form').modal('show')
})
document.getElementById('project_join_button').addEventListener('click', function () {
    $('#project_join_form').modal('show')
})

////////////////////////组长：申请项目 begin///////////////////
var addGroup = function (){
    var leaderID = sessionStorage.getItem('user_id');
    var name = $("#project_name").val();
    var property = $("#project_money").val();
    var dueTime = $("#project_lasttime").val();
    var workload = $("#project_worknum").val();

    if(name == "" || property == "" || dueTime == "" || workload == "")
    {
        alert("请完善申请信息！");
        return;
    }
    if(property > 2147483647 || property < 0)
    {
        alert("金额溢出！");
        return;
    }
    var myDate = new Date();
    var startTime = myDate.toLocaleDateString();
    startTime = startTime.replace(/\//g,"-");
    data = {}
    data.leaderID = leaderID;
    data.name = name;
    data.property = property;
    data.dueTime  = dueTime;
    data.workload = workload;
    data.startTime = startTime;
    data.leader_name = sessionStorage.getItem('user_name');
    $.post("./project/addGroup", data, function (json) {
        json = JSON.parse(json);
        if (json.result_msg === 1) {
            $('#project_add_form').modal('hide');
            alert("项目添加成功！");
            showPage();
        }
        else {
            alert("项目添加失败！");
        }
    })
}
////////////////////////组长：申请项目 end///////////////////
